package com.tengju.bff.interfaces.manage.crm.activity;

import com.tengju.bff.interfaces.manage.common.facade.CommonManageFacade;
import com.tengju.bff.interfaces.manage.crm.activity.facade.ActivityTargetAnalysisFacade;
import com.tengju.bff.interfaces.manage.crm.activity.form.*;
import com.tengju.bff.interfaces.manage.crm.activity.vo.ActivityCalendarVO;
import com.tengju.bff.interfaces.manage.crm.activity.vo.SaleTargetValueVO;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.KVVO;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.bff.interfaces.shared.render.FunnelVO;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.data.domain.model.clickhouse.crm.UserRole;
import com.tengju.export.application.ExportService;
import com.tengju.export.domain.record.ExportDataType;
import com.tengju.export.domain.record.ExportFileType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author chenhang
 * @Date 2021/10/13
 */
@Validated
@RestController
@Api(tags = "生意参谋-销售活动目标-服务商视角")
@RequestMapping(value = "manage/crm/activity/analysis/serviceProvider")
@Slf4j
@ManageEntrance
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ActivityServicerProviderAnalysisController {

    private final ActivityTargetAnalysisFacade activityTargetAnalysisFacade;
    private final ExportService exportService;
    private final CommonManageFacade commonManageFacade;

    /******************************           （服务商视角）活动查看         *********************************/


    @ApiOperation(value = "（服务商）活动目标概览", notes = "销售目标概览")
    @RequestMapping(value = "/serviceProviderOverview", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO>> serviceProviderOverview(@RequestBody @Valid ActivityTeamQuery query) {
        query.setUserRole(UserRole.SERVICE_PROVIDER);
        return ApiResponse.newSuccess(activityTargetAnalysisFacade.overview(query));
    }

    @ApiOperation(value = "（服务商）活动目标table", notes = "销售目标table")
    @RequestMapping(value = "/serviceProviderTargetTable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<TableVO> serviceProviderTargetTable(@RequestBody @Valid ActivityTeamQuery query) {
        query.setUserRole(UserRole.SERVICE_PROVIDER);
        return ApiResponse.newSuccess(activityTargetAnalysisFacade.overviewTable(query));
    }

    @ApiOperation(value = "（服务商）活动转化漏斗", notes = "销售目标table")
    @RequestMapping(value = "/serviceProviderTransformFunnel", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO>> serviceProviderTransformFunnel(@RequestBody @Valid ActivityTeamQuery query) {
        query.setUserRole(UserRole.SERVICE_PROVIDER);
        return ApiResponse.newSuccess(activityTargetAnalysisFacade.transformFunnel(query));
    }

    @ApiOperation(value = "（服务商）加购商品排行榜", notes = "销售目标table")
    @RequestMapping(value = "/serviceProviderAddcartGoodsRankTable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<TableVO> serviceProviderAddcartGoodsRankTable(@RequestBody @Valid ActivityTeamQuery query) {
        query.setUserRole(UserRole.SERVICE_PROVIDER);
        return ApiResponse.newSuccess(activityTargetAnalysisFacade.addcartGoodsRankTable(query));
    }

    @ApiOperation(value = "（服务商）出货商品排行榜", notes = "销售目标table")
    @RequestMapping(value = "/serviceProviderBuyGoodsRankTable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<TableVO> serviceProviderBuyGoodsRankTable(@RequestBody @Valid ActivityTeamQuery query) {
        query.setUserRole(UserRole.SERVICE_PROVIDER);
        return ApiResponse.newSuccess(activityTargetAnalysisFacade.moneyoutRankTable(query));
    }

    @ApiOperation(value = "（服务商）经销商社群贡献排行榜", notes = "销售目标table")
    @RequestMapping(value = "/serviceProviderDealerRankTable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<TableVO> serviceProviderDealerRankTable(@RequestBody @Valid ActivityTeamQuery query) {
        query.setUserRole(UserRole.SERVICE_PROVIDER);
        return ApiResponse.newSuccess(activityTargetAnalysisFacade.dealerRankTable(query));
    }

    @ApiOperation(value = "sec目标达成", notes = "sec目标达成")
    @RequestMapping(value = "/secTargetReachTable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<TableVO> secTargetReachTable(@RequestBody @Valid ActivityTeamQuery query) {
        query.setUserRole(UserRole.SEC);
        return ApiResponse.newSuccess(activityTargetAnalysisFacade.secTargetReachTable(query));
    }

    @ApiOperation(value = "服务商目标达成", notes = "服务商目标达成")
    @RequestMapping(value = "/serviceProviderTargetReachTable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<TableVO> serviceProviderTargetReachTable(@RequestBody @Valid ActivityTeamQuery query) {
        query.setUserRole(UserRole.SERVICE_PROVIDER);
        return ApiResponse.newSuccess(activityTargetAnalysisFacade.serviceProviderTargetReachTable(query, "activity_target_complete_service_provider_table"));
    }

    @ApiOperation(value = "下载sec目标达成", notes = "下载sec目标达成")
    @RequestMapping(value = "/exportSecTargetReachTable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<Object> exportSecTargetReachTable(@RequestBody @Valid ActivityTeamQuery query) {
        query.setUserRole(UserRole.SEC);
        long staffId = ServletContextUtil.getServletContext().getStaffId().getValue();
        String realName = ServletContextUtil.getServletContext().getRealName();
        exportService.exportData("sec目标达成数据导出", ExportFileType.XLSX,
                "secTargetReachExportDataSourceRepository",
                ExportDataType.TABLE, query,
                staffId, realName);
        return ApiResponse.newSuccess();
    }

    @ApiOperation(value = "下载服务商目标达成", notes = "下载服务商目标达成")
    @RequestMapping(value = "/exportServiceProviderTargetReachTable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<Object> exportServiceProviderTargetReachTable(@RequestBody @Valid ActivityTeamQuery query) {
        query.setUserRole(UserRole.SERVICE_PROVIDER);
        long staffId = ServletContextUtil.getServletContext().getStaffId().getValue();
        String realName = ServletContextUtil.getServletContext().getRealName();
        exportService.exportData("服务商目标达成数据导出", ExportFileType.XLSX,
                "serviceProviderTargetReachExportDataSourceRepository",
                ExportDataType.TABLE, query,
                staffId, realName);
        return ApiResponse.newSuccess();

    }

}
